package org.appfuse.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.dao.DutyExperienceDAO;
import org.appfuse.model.DutyExperience;
import org.hibernate.FlushMode;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class DutyExperience.
 *  
 * @see temp.DutyExperience
 * @author MyEclipse - Hibernate Tools
 */
public class DutyExperienceDAOHibernate extends BaseDaoHibernate implements
		DutyExperienceDAO {

	private static final Log log = LogFactory
			.getLog(DutyExperienceDAOHibernate.class);


	public static final String A00 = "a00";

	public void save(DutyExperience transientInstance) {
		log.debug("saving DutyExperience instance");
		try {
			getSession().setFlushMode(FlushMode.AUTO);
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void update(DutyExperience transientInstance) {
		log.debug("saving DutyExperience instance");
		try {
			getSession().setFlushMode(FlushMode.AUTO);
			getSession().update(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(DutyExperience persistentInstance) {
		log.debug("deleting DutyExperience instance");
		try {
			getSession().setFlushMode(FlushMode.AUTO);
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DutyExperience findById(String a00, Integer id) {
		log.debug("getting DutyExperience instance with id: " + id);
		try {
			String queryString = "from DutyExperience as model where model.basicInfo.id"
				 + "= ? " + " and model.id = ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, a00);
			queryObject.setParameter(1, id);
			return (DutyExperience) queryObject.uniqueResult();
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	

	public Integer newId(String a00){
		log.debug("generate a new id");
		try {
			String queryString = "select max(id) from DutyExperience as model where model.basicInfo.id "
				 + "= ?";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setParameter(0, a00);
		Integer maxId = (Integer) queryObject.uniqueResult();
		if (maxId != null){
			maxId = maxId + 1;
		}else{
			maxId = 1;
		}
		log.debug("generate new id successful");
		return maxId;
			
		} catch (RuntimeException re) {
			log.error("generate new id failed", re);
			throw re;
		}
	}

	public List findByA00(String a00) {
		log.debug("generate a new id");
		try {
			String queryString = " from DutyExperience as model where model.basicInfo.id "
				 + "= ?";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setParameter(0, a00);
		return queryObject.list();
		} catch (RuntimeException re) {
			log.error("generate new id failed", re);
			throw re;
		}
	}

	public List findByExample(DutyExperience instance) {
		log.debug("finding DutyExperience instance by example");
		try {
			List results = getSession().createCriteria("temp.DutyExperience")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding DutyExperience instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from DutyExperience as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByRzdw(Object rzdw) {
		return new ArrayList(0);
	}

	public List findByZwmc(Object zwmc) {
		return new ArrayList(0);
	}

	public List findByZwlb(Object zwlb) {
		return new ArrayList(0);
	}

	public List findByDzwxh(Object dzwxh) {
		return new ArrayList(0);
	}

	public List findByJtnpx(Object jtnpx) {
		return new ArrayList(0);
	}

	public List findByZggz(Object zggz) {
		return new ArrayList(0);
	}

	public List findByUserid(Object userid) {
		return new ArrayList(0);
	}

	public List findByZj(Object zj) {
		return new ArrayList(0);
	}

	public DutyExperience merge(DutyExperience detachedInstance) {
		log.debug("merging DutyExperience instance");
		try {
			DutyExperience result = (DutyExperience) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(DutyExperience instance) {
		log.debug("attaching dirty DutyExperience instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(DutyExperience instance) {
		log.debug("attaching clean DutyExperience instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

}